<?php
	/**
	 * Defines the basic implementation of a Krome application.
	 * 
	 * The basic structure of the object that all applications
	 * in Krome inherit from.
	 *
	 * @author     Brandon R. Stoner <brandon.stoner@gmail.com>
	 * @copyright  Copyright 2008, Monokromatic Inc.
	 * @package    Applications
	 * @subpackage Core	 	 
	 **/	 	

	/**
	 * Base implementation of an application.
	 * 
	 * Functionality inherited by every application in Krome.
	 *
	 * @author     Brandon R. Stoner <brandon.stoner@gmail.com>
	 * @copyright  Copyright 2008, Monokromatic Inc.	 
	 * @package     Applications
	 * @subpackage	Core 	 	 	 	 
	 **/
	abstract class IKromeApplication extends IKromeBase
	{
		/**
		 * A "friendly" name for this object.
		 * @var string
		 **/		 		
		public  $name              = 'Krome Base Application';

		/**
		 * An array containing a list of components that should be found or
		 * instantiated for the use of this application (or any other that may
		 * realize it is existant now)	
		 * @var array Array of strings.		 	 		 
		 **/		 		
		public $initialComponents  = Array();

		/**
		 * Reference to the component manager that this application should be
		 * utilizing.
		 * @var object A component manager object. {@see config.php}
		 **/		 		
		private $componentManager  = null;

		/**
		 * Used for initializing any resources that the application wants to have
		 * access to. This method shouldn't access any other applications, as there
		 * is no guarantee that the other applications have been initialized yet.
		 * @return bool False if initialization of the application fails.		 		 		 
		 **/		 		
		         public function Initialize() {}

		/**
		 * Used to initialize any bridging between applications, as any created
		 * application will have also been initialized at this point in the process.
		 * @return bool False if creation of the application fails.	 
		 **/
		abstract public function Create();

		/**
		 * General application execution function. After resources are initialized,
		 * this method will be called. Depending where this application is ran, this
		 * method can be called any amount of times.
		 * @return bool False if this application is finished executing.		 		 		 		 
		 **/		 		
		abstract public function Execute();

		/**
		 * This method is where any cleaning up of bridging between multiple
		 * applications should be happening.
		 **/		 		
		abstract public function Cleanup();

		/**
		 * Used for cleaning up any other resources that may have been allocated for
		 * this application at any point in time.		  
		 **/		 		
		         public function Destroy()    {}

		/**
		 * Sets the application's component manager if provided, otherwise creates
		 * a new one. Attempts to load any of the application's required components.
		 * 		 
		 * @param object The component manager that this application should use.
		 **/
		public function __construct( $pComponentManager = null )
		{
			$this->componentManager = $pComponentManager;

			if ( $this->componentManager === null )
			{
				$componentManagerClassName = KROME_COMPONENTMANAGER_CLASSNAME;
				$this->componentManager = new $componentManagerClassName();
			}

			if ( $this->componentManager !== null )
			{
				foreach ( $this->initialComponents as $componentName )
				{
					$this->componentManager->Load( $componentName );
				}
			}

			if ( $this->Initialize() !== false )
			{
				$this->Create();
			}
		}

		/**
		 * Calls the application's Destroy() method.
		 **/		 		
		public function __destruct()
		{
			$this->CleanUp();
			$this->Destroy();
		}

		/**
		 * Returns a reference to the application's component manager.
		 * @returns mixed The component manager, if exists. Otherwise, NULL.		 
		 **/		 		
		public function GetComponentManager()
		{
			return $this->componentManager;
		}
	}
?>
